
#include "app_uart.h"
#include "OKIN_Global.h"
#include "nrf_uarte.h"
#include "OKIN_app_headall.h"
#include "OKIN_drive_usart.h"
#include "nrf_drv_uart.h"

static nrf_drv_uart_t app_uart_inst = NRF_DRV_UART_INSTANCE(APP_UART_DRIVER_INSTANCE);

void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[1];


    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[0]));
						OKIN_MFP_dateReceive(data_array[0]);
            break;

        case APP_UART_COMMUNICATION_ERROR:
           // APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            //APP_ERROR_HANDLER(p_event->data.error_code);
            break;
				case APP_UART_TX_EMPTY:
						OKIN_drive_uart_RXenable();
						break;

        default:
            break;
    }
}


void OKIN_drive_uart_init(void)
{
    uint32_t                     err_code;
    app_uart_comm_params_t const comm_params =
    {
        .rx_pin_no    = UART_RX_PIN,
        .tx_pin_no    = UART_TX_PIN,
        .rts_pin_no   = UART_PIN_DISCONNECTED,
        .cts_pin_no   = UART_PIN_DISCONNECTED,
        .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
        .use_parity   = true,
        .baud_rate    = NRF_UARTE_BAUDRATE_38400

    };

    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_event_handle,
                       _PRIO_APP_HIGH,
                       err_code);
    APP_ERROR_CHECK(err_code);
}
/**@snippet [UART Initialization] */







uint32_t app_uart_close1(void)
{
		 nrf_drv_uart_uninit(&app_uart_inst);    
    return NRF_SUCCESS;
}
/**@snippet [UART Initialization] */



void OKIN_drive_uart_RXenable(void)
{
	nrf_gpio_pin_set(UART_CTR_PIN);
}

void OKIN_drive_uart_TXenable(void)
{
	nrf_gpio_pin_clear(UART_CTR_PIN);	
}

void OKIN_drive_uart_send(uint8_t *p, uint8_t len)
{
	OKIN_drive_uart_TXenable();
	int i = 0;
	for(i = 0; i < len;i++)
	{
		app_uart_put(p[i]);
	}
		//OKIN_drive_uart_RXenable();
}
